package com.example.quartz.controller;

import com.example.quartz.service.QuartzService;
import com.example.quartz.to.QuartzTo;
import com.example.quartz.util.ResponseMessage;
import com.example.quartz.vo.QuartzVo;
import org.quartz.CronExpression;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author 杨豹
 * @create 2021/8/12 0012 12:06
 * @description 调度任务信息操作处理
 */
@RestController
@RequestMapping("/quartz")
public class QuartzController {
    @Autowired
    QuartzService quartzService;

    /**
     * 查询定时任务列表
     */
    @PostMapping("/list")
    public ResponseMessage<List<QuartzTo>> list(@RequestBody QuartzVo quartz) {
        return ResponseMessage.ok(quartzService.selectQuartzList(quartz));
    }

    /**
     * 新增定时任务
     */
    @PostMapping("insert")
    public ResponseMessage<String> add(@RequestBody QuartzVo vo){
        //判断表达式是否正确
        if (!CronExpression.isValidExpression(vo.getExchangeCron())) {
            return ResponseMessage.error("cron表达式不正确");
        }
        if (Objects.equals(quartzService.add(vo), 1)) {
            return ResponseMessage.ok("新建成功");
        }
        return ResponseMessage.error("新建失败");
    }
}
